package org.jeecg.modules.app.order.service.impl;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.app.order.service.AppMemberOrderService;
import org.jeecg.modules.app.order.vo.AppMemberOrderEnt;
import org.jeecg.modules.power.group.service.IYlGroupService;
import org.jeecg.modules.power.order.mapper.YlMemberOrderMapper;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @BelongsProject: jeecg-boot-parent app运营端 获取会员充值订单 业务实现层
 * @BelongsPackage: org.jeecg.modules.app.order.service.impl
 * @Author: mi
 * @CreateTime: 2024-07-17 10:16
 * @Description:
 */
@Service
public class AppMemberOrderServiceImpl implements AppMemberOrderService {


    @Autowired
    private YlMemberOrderMapper ylMemberOrderMapper;//会员充值订单
    //小组
    @Autowired
    private IYlGroupService ylGroupService;


    @Autowired
    private RedisUtil redisUtil;//redis
    /**
     * APP运营端-获取会员订单（今日、昨日、本周、本月）
     * @param type 时间类型（1今日、2昨日、3本周、4本月）
     * @return
     */
    @Override
    public AppMemberOrderEnt getAppMemberOrderEntCycle(SysUser user,Integer type) {

        List<String> stringList=new LinkedList<>();

        //判断是不是组长 是否组长1是2否
        if (user.getIsLeaders().equals("1")){
            stringList=ylGroupService.getGroupUserById(user.getGroupIds());
        }else {
            //根据群组id 获取数据id
            stringList.add(user.getId());
        }

        String ids = String.format("'%s'", String.join("','", stringList));



        AppMemberOrderEnt appMemberOrderEnt=    ylMemberOrderMapper.getAppMemberOrderEntCycle(ids,type);
        appMemberOrderEnt.setAppMemberOrderGroupingList(ylMemberOrderMapper.getAppMemberOrderGroupingList(ids,type));//会员卡订单分组统计
        appMemberOrderEnt.setAppMemberOrderList(ylMemberOrderMapper.getAppMemberOrderList(ids,type));//会员卡订单数据
        return appMemberOrderEnt;
    }

    /**
     * APP运营端-获取会员订单折线图
     * @param cycleTime 时间周期近 （1 、30天 2、 6个月)
     * @return
     */
    @Override
    public Result<?> getAppMemberOrderLineChart(SysUser user,Integer cycleTime) {
        SimpleDateFormat formats = new SimpleDateFormat("MM.dd HH:mm");
        String dateStr= formats.format(new Date());//当前请求时间
        Map map=new HashMap();

        String key= "getAppMemberOrderLineChart"+user.getId()+cycleTime;//缓存key
        Object object = redisUtil.get(key);
        if (object != null) {
            map=Map.class.cast(object);
            return Result.OK(map);
        }
        List<String> stringList=new LinkedList<>();
        //判断是不是组长 是否组长1是2否
        if (user.getIsLeaders().equals("1")){
            stringList=ylGroupService.getGroupUserById(user.getGroupIds());
        }else {
            //根据群组id 获取数据id
            stringList.add(user.getId());
        }

        String ids = String.format("'%s'", String.join("','", stringList));
        //

        List<Map>mapList=new LinkedList<>();
        AppMemberOrderEnt daysEnt=    ylMemberOrderMapper.getAppMemberOrderEntCycle(ids,1);//今日
        Map map1=new HashMap();
        map1.put("type",1);
        map1.put("payPrice",daysEnt.getPayPrice());
        map1.put("num",daysEnt.getNum());
        mapList.add(map1);
        AppMemberOrderEnt benDayEnt=    ylMemberOrderMapper.getAppMemberOrderEntCycle(ids,3);//3本周
        Map map2=new HashMap();
        map2.put("type",3);
        map2.put("payPrice",benDayEnt.getPayPrice());
        map2.put("num",benDayEnt.getNum());
        mapList.add(map2);
        AppMemberOrderEnt yueEnt=    ylMemberOrderMapper.getAppMemberOrderEntCycle(ids,4);//4本月
        Map map3=new HashMap();
        map3.put("type",4);
        map3.put("payPrice",benDayEnt.getPayPrice());
        map3.put("num",benDayEnt.getNum());
        mapList.add(map3);
        map.put("payTime",dateStr);
        map.put("memberOrderList",mapList);
        if (cycleTime==1){//近30天
            //获取近30天的日期区间
            Calendar calendar = new GregorianCalendar();
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MILLISECOND,0);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            Date dayEnd = calendar.getTime();
            calendar.add(Calendar.DAY_OF_MONTH, -30);
            Date dayStart = calendar.getTime();
            map.put("AppMemberOrderDayList",ylMemberOrderMapper.getAppMemberOrderDayList(ids,dayStart, dayEnd));
        }else {//近六个月
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MILLISECOND,0);
            calendar.set(Calendar.DAY_OF_MONTH,1);
            calendar.add(Calendar.MONTH, -6);
            Date dayStartYue = calendar.getTime();//开始时间
            map.put("AppMemberOrderDayList",ylMemberOrderMapper.getAppMemberOrderYueList(ids,dayStartYue, new Date()));
        }

        //存储缓存 redis 缓存数据 5分钟内有效
        redisUtil.set(key, map, 300);

        return Result.OK(map);
    }
}
